/**
 * 
 */
package loansManagement;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import rappelEmail.SendEmail;

import daos.DAOs;

/**
 * @author virgil
 *send email when return date is passed
 */
public class LoanReminder {
	Timer timer;
	
	 public LoanReminder () {
		 Calendar calendar = Calendar.getInstance();
		 calendar.set(Calendar.HOUR_OF_DAY, 10);
		 calendar.set(Calendar.MINUTE, 00);
		 calendar.set(Calendar.SECOND, 0);
		 Date time = calendar.getTime();
		 System.out.println(time);
	        timer = new Timer();
	    	
	        timer.schedule(new TimerTask() {
	        	 List<Loan> loans = (ArrayList<Loan>) DAOs.getLoanDAO().findAll();
				@Override
				public void run() {
					// TODO Auto-generated method stub
				
					 for(Loan l:loans){
						 
						 if(!l.isIsReturn()&&l.getReturnDate().getTime().before(new GregorianCalendar().getTime()))
								 SendEmail.sendLoanMail(l);
						 
					 }
				}
			}, 5000);
		}
	 
	 
	 class LoanTask extends TimerTask{
		 List<Loan> loans = (ArrayList<Loan>) DAOs.getLoanDAO().findAll();
		 @Override
			public void run() {
				// TODO Auto-generated method stub
			System.out.println("essai reussi");
			 for(Loan l:loans){
				 
				 if(!l.isIsReturn()&&l.getReturnDate().getTime().before(new GregorianCalendar().getTime()))
						 SendEmail.sendLoanMail(l);
				 
			 }
			//timer.cancel();	
			}
	 }

	
}
